package com.cupk.carManageMaster.core.serviceImpl;
/**
 * @Software: IntelliJ IDEA
 * @Project: car-manage-master
 * @File: AdministrationServiceImpl.java
 * @Author: 箬玉
 * @Description: 行政接口实现
 * @Date: 2023/6/9 22:01
 */

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cupk.carManageMaster.common.domain.ResponseBase;
import com.cupk.carManageMaster.common.dto.*;
import com.cupk.carManageMaster.common.enums.ErrorEnum;
import com.cupk.carManageMaster.common.mapper.AdministrationMapper;
import com.cupk.carManageMaster.core.service.AdministrationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: AdministrationServiceImpl
 * @Author: 箬玉
 * @Description: 行政接口实现
 * @Date: 2023/6/9 22:01
 */
@Service
@Transactional
@Slf4j
public class AdministrationServiceImpl extends BaseApiServiceImpl implements AdministrationService {
    private final AdministrationMapper administrationMapper;

    public AdministrationServiceImpl(AdministrationMapper administrationMapper) {
        this.administrationMapper = administrationMapper;
    }

    /**
     * 车辆信息批量上传
     *
     * @param excelData
     * @return
     */
    @Override
    public ResponseBase uploadCar(String excelData) {
        JSONObject jsonData = JSONObject.parseObject(excelData);
        JSONArray jsonDataArray = jsonData.getJSONArray("excelData");
        int count = 0;
        List<String> carNumberList = new ArrayList<>();
//        Map<String, String> errorMsg = new HashMap<>();
        for (int i = 0; i < jsonDataArray.size(); i++) {
            Long typeCarId = getCarType(jsonDataArray.getJSONObject(i).getString("type_car"));
            String carNumber = jsonDataArray.getJSONObject(i).getString("car_number");
            if (typeCarId == 1000L || typeCarId == 100L) {
                carNumberList.add(carNumber);
                continue;
            }
            if (administrationMapper.isExistCarNumber(carNumber) != 0) {
                carNumberList.add(carNumber);
                continue;
            }
            count++;
            try {
                administrationMapper.uploadCar(carNumber, typeCarId);
            } catch (Exception e) {
                count--;
                carNumberList.add(carNumber);
//                errorMsg.put("errorMsg", e.toString());
//                return setResultError(e.toString());
            }
        }
        return setResult(200, Integer.toString(count), carNumberList);
//        if (errorMsg.isEmpty()) {
//            return setResult(200, Integer.toString(count), errorMsg);
//        } else {
//            return setResultSuccess(Integer.toString(count));
//        }
    }

    /**
     * 车辆信息修改
     *
     * @param objectData
     * @return
     */
    @Override
    public ResponseBase changeCar(String objectData) {
        JSONObject jsonData = JSONObject.parseObject(objectData);
        String typeCar = jsonData.getString("type_car");
        Long typeCarId = getCarType(typeCar);
        if (typeCarId == 1000L || typeCarId == 100L) {
            log.error(ErrorEnum.CAR_TYPE_ERROR.getMessage());
            return setResultError(ErrorEnum.CAR_TYPE_ERROR.getMessage());
        }
        Long id = jsonData.getLong("id");
        String carNumber = jsonData.getString("car_number");
        Date purchaseDate = new Date(jsonData.getLong("purchase_date"));
        try {
            administrationMapper.changeCar(id, carNumber, purchaseDate, typeCarId);
        } catch (Exception e) {
            return setResultError(e.toString());
        }
        return setResultSuccess();
    }

    /**
     * 车辆信息列表
     *
     * @return 车辆信息列表
     */
    @Override
    public ResponseBase getCarList() {
        List<CarMessageDto> carMessageList;
        try {
            carMessageList = administrationMapper.getCarList();
        } catch (Exception e) {
            return setResultError(e.toString());
        }
        if (carMessageList.size() == 0) {
            log.error(ErrorEnum.SQL_EMPTY_ERROR.getMessage());
            return setResultError(ErrorEnum.SQL_EMPTY_ERROR.getMessage());
        } else {
            return setResultSuccess(carMessageList);
        }
    }

    /**
     * 车辆近期调度信息
     *
     * @param carId
     * @return
     */
    @Override
    public ResponseBase getCarDispatch(Long carId) {
        List<CarDispatchDto> carDispatchList;
        try {
            carDispatchList = administrationMapper.getCarDispatch(carId);
        } catch (Exception e) {
            return setResultError(e.toString());
        }
        if (carDispatchList.size() == 0) {
            log.error(ErrorEnum.SQL_EMPTY_ERROR.getMessage());
            return setResultError(ErrorEnum.SQL_EMPTY_ERROR.getMessage());
        } else {
            return setResultSuccess(carDispatchList);
        }
    }

    /**
     * 车辆信息删除
     *
     * @param id
     * @return
     */
    @Override
    public ResponseBase deleteCar(Long id) {
        try {
            administrationMapper.deleteCar(id);
        } catch (Exception e) {
            return setResultError(e.toString());
        }
        return setResultSuccess();
    }

    /**
     * 车辆当前工作状态列表
     *
     * @return
     */
    @Override
    public ResponseBase getCarNow() {
        List<CarMessageDto> carMessageList;
        try {
            carMessageList = administrationMapper.getCarList();
//            System.out.println(carMessageList.get(0).toString());
        } catch (Exception e) {
            return setResultError(e.toString());
        }
        if (carMessageList.size() == 0) {
            log.error(ErrorEnum.SQL_EMPTY_ERROR.getMessage());
            return setResultError(ErrorEnum.SQL_EMPTY_ERROR.getMessage());
        } else {
            JSONArray jsonArray = new JSONArray();
            for (CarMessageDto carMessageDto : carMessageList) {
                JSONObject objects = new JSONObject();
                objects.put("car_message", carMessageDto);
//                System.out.println(objects);
                CarDispatchDto carDispatchDto;
                try {
                    carDispatchDto = administrationMapper.getCarDispatchNow(carMessageDto.getId());
                } catch (Exception e) {
                    return setResultError(e.toString());
                }
                if (carDispatchDto == null) {
                    continue;
                }
                objects.put("car_dispatch", carDispatchDto);
                DriverDto driverDto;
                try {
                    driverDto = administrationMapper.getDriver(carDispatchDto.getDriverId());
                } catch (Exception e) {
                    return setResultError(e.toString());
                }
                objects.put("driver_message", driverDto);
                jsonArray.add(objects);
//                System.out.println(jsonArray);
//                objects.clear();
            }
//            System.out.println(jsonArray);
            return setResultSuccess(jsonArray);
        }
    }

    /**
     * 车型信息列表
     *
     * @return 车型信息列表
     */
    @Override
    public ResponseBase carTypeList() {
        List<CarTypeDto> carTypeDto;
        try {
            carTypeDto = administrationMapper.carTypeList();
        } catch (Exception e) {
            return setResultError(e.toString());
        }
        if (carTypeDto.size() == 0) {
            log.error(ErrorEnum.SQL_EMPTY_ERROR.getMessage());
            return setResultError(ErrorEnum.SQL_EMPTY_ERROR.getMessage());
        } else {
            return setResultSuccess(carTypeDto);
        }
    }

    /**
     * 根据车型查询空闲车辆
     *
     * @param typeCarId
     * @return
     */
    @Override
    public ResponseBase getCarFree(Long typeCarId) {
        List<CarMessageDto> carMessageList;
        try {
            carMessageList = administrationMapper.getCarFree(typeCarId);
        } catch (Exception e) {
            return setResultError(e.toString());
        }
        if (carMessageList.size() == 0) {
            return setResultError("0");
        } else {
            return setResultSuccess(carMessageList);
        }
    }

    /**
     * 根据车型查询空闲司机
     *
     * @param typeCarId
     * @return
     */
    @Override
    public ResponseBase getDriverFree(Long typeCarId) {
        List<DriverDto> driverFreeList;
        try {
            driverFreeList = administrationMapper.getDriverFree(0, typeCarId);
        } catch (Exception e) {
            return setResultError(e.toString());
        }
        if (driverFreeList.size() == 0) {
            return setResultError("0");
        } else {
            return setResultSuccess(driverFreeList);
        }
    }

    /**
     * 车辆任务分配
     *
     * @param objectData
     * @return
     */
    @Override
    public ResponseBase carTask(String objectData) {
        JSONObject jsonData = JSONObject.parseObject(objectData);
        Long carId = jsonData.getLong("car_id");
        Long driverId = jsonData.getLong("driver_id");
        Date scheduleStartTime = jsonData.getDate("schedule_start_time");
        Date expectedScheduleEndTime = jsonData.getDate("expected_schedule_end_time");
        String message = jsonData.getString("message");
        Long mileage = jsonData.getLong("mileage");
        try {
            administrationMapper.carTask(carId);
            administrationMapper.driverTask(driverId);
            administrationMapper.dispatchCar(mileage, driverId, scheduleStartTime, expectedScheduleEndTime, message, carId);
        } catch (Exception e) {
            return setResultError(e.toString());
        }
        return setResultSuccess();
    }

    /**
     * 求助信息列表
     *
     * @return
     */
    @Override
    public ResponseBase getResort() {
        List<MessageHelpDto> messageHelpList;
        try {
            messageHelpList = administrationMapper.getResortList();
        } catch (Exception e) {
            return setResultError(e.toString());
        }
        if (messageHelpList.size() == 0) {
            return setResultError("0");
        } else {
            return setResultSuccess(messageHelpList);
        }
    }

    /**
     * 根据车型文字查询所代表的数字
     *
     * @param typeCar
     * @return 汽车车型代表的数字
     */
    private Long getCarType(String typeCar) {
        Long carTypeId;
        try {
            carTypeId = administrationMapper.getCarTypeId(typeCar);
        } catch (Exception e) {
//            System.out.println(e.toString());
            carTypeId = 1000L;
        }
        if (carTypeId == null) {
            carTypeId = 100L;
        }
        return carTypeId;
    }


}
